# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=opencollada
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=r1687.d826fd08
pkgrel=1
pkgdesc="Stream based reader and writer library for COLLADA files (mingw-w64)"
arch=('any')
url="https://github.com/khronosGroup/OpenCOLLADA/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-cmake" "git")
depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-pcre")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('staticlibs' 'strip')
source=("${_realname}"::"git+https://github.com/KhronosGroup/OpenCOLLADA.git"
        fix-mingw-w64.patch
        fix-missing-include.patch
        fix-inttypes-mingw.patch
        fix-pcre-type-conflict.patch
        opencollada-cmake.patch
        opencollada-disable-test-mingw.patch)
sha256sums=('SKIP'
            '37e70a63264974c31fde4a94908a60954af454fd28412bd141c41aac9fcbd4c1'
            '18f1bf9f57d1c67aa35927fb0d1b0b38def52b189742021f1268eb2bdbffc963'
            '6fb7cf3116bdcb93af58a2487c44b298afd11e83c21ec87a8c921eda6dcba766'
            'ecbe4863c23e1296d6dd16a290e478f2627165626330153591e3cc5e98025c2c'
            '928503c46b3b6324986c2a3d8ca1b79597984e559e6d233dfec2e669b772fe20'
            '51306dc69f4f30536be9bb1beb63a88b524a297f9f58297c60fa2e34f14cb90a')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"
  patch -p0 -i ${srcdir}/fix-mingw-w64.patch
  patch -p1 -i ${srcdir}/fix-missing-include.patch
  patch -p1 -i ${srcdir}/fix-inttypes-mingw.patch
  patch -p1 -i ${srcdir}/fix-pcre-type-conflict.patch
  patch -p0 -i ${srcdir}/opencollada-cmake.patch
  patch -p0 -i ${srcdir}/opencollada-disable-test-mingw.patch
}

build() {
  [[ -d "${srcdir}"/build-${MINGW_CHOST} ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}
  mkdir -p "${srcdir}"/build-${MINGW_CHOST}
  cd "${srcdir}"/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
     -G"MSYS Makefiles" \
     -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
     -DCMAKE_BUILD_TYPE=Release \
     ../${_realname}
  make
}

package() {
  cd "${srcdir}"/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  # replace absolute paths in .cmake scripts
  sed -i '1i get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../" ABSOLUTE)' "${pkgdir}${MINGW_PREFIX}/lib/cmake"/*Targets*.cmake
  sed -i "s;$(cygpath -ma "${pkgdir}${MINGW_PREFIX}");\${PACKAGE_PREFIX_DIR};" "${pkgdir}${MINGW_PREFIX}/lib/cmake"/*Targets*.cmake
}
